package com.xju.subjectsystem.service.impl;

import com.xju.subjectsystem.mapper.TeacherMapper;
import com.xju.subjectsystem.pojo.LoginInfo;
import com.xju.subjectsystem.pojo.Result;
import com.xju.subjectsystem.pojo.Teacher;
import com.xju.subjectsystem.service.TeacherService;
import com.xju.subjectsystem.utils.JwtUtil;
import com.xju.subjectsystem.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.service.impl
 * @className: TeacherServiceImpl
 * @author: Eiard
 * @description: TODO : 教师实现类
 * @date: 2024-12-03 19:42
 */
@Service
public class TeacherServiceImpl implements TeacherService {


    private final TeacherMapper teacherMapper;

    public TeacherServiceImpl(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }


    @Override
    public Teacher selectByUsername(String username) {

        Teacher teacher = teacherMapper.selectByPrimaryKey(username);

        if (teacher != null) {
            teacher.setTpwd(null);
        }

        return teacher;
    }

    @Override
    public void deleteByUsername(String username) {
        teacherMapper.updateAccountStatusByPrimaryKey(username, 0);
    }

    @Override
    public void add(Teacher teacher) throws NoSuchAlgorithmException {
        teacher.setTpwd(Md5Util.encrypt(teacher.getTpwd()));
        teacherMapper.insert(teacher);
    }

    @Override
    public void addSelective(Teacher teacher) throws NoSuchAlgorithmException {
        teacher.setTpwd(Md5Util.encrypt(teacher.getTpwd()));
        teacherMapper.insertSelective(teacher);
    }


    @Override
    public Map<String, List<String>> batchAddTeachers(List<Teacher> teacherList) {
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();

        for (Teacher teacher : teacherList) {
            try {
                this.addSelective(teacher); // 调用Service方法处理单个教师的插入和账号创建
                successList.add("教师: " + teacher.getTname() + " 导入成功");
            } catch (Exception e) {
                failList.add("教师: " + teacher.getTname() + " 导入失败，原因:数据异常");
            }
        }

        // 将结果封装成Map返回
        Map<String, List<String>> resultData = new HashMap<>();
        resultData.put("successList", successList);
        resultData.put("failList", failList);

        return resultData;
    }

    @Override
    public void update(Teacher teacher) {
        teacherMapper.updateByPrimaryKey(teacher);
    }

    @Override
    public void updateSelective(Teacher teacher) {
        teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    public void updateTeacher(Teacher teacher) {

        Teacher teacher1 = new Teacher();
        teacher1.setTno(teacher.getTno()); // tno 是更新条件，必须保留

        // 只允许更新的字段
        if (teacher.getTpwd() != null) {
            teacher1.setTpwd(teacher.getTpwd());
        }
        if (teacher.getTel() != null) {
            teacher1.setTel(teacher.getTel());
        }
        if (teacher.getTmail() != null) {
            teacher1.setTmail(teacher.getTmail());
        }
        if (teacher.getWechat() != null) {
            teacher1.setWechat(teacher.getWechat());
        }
        if (teacher.getPicture() != null) {
            teacher1.setPicture(teacher.getPicture());
        }

        this.updateSelective(teacher1);
    }


    @Override
    public LoginInfo login(String username, String password) throws NoSuchAlgorithmException {

        // 根据用户名或手机号查找教师信息
        Teacher teacherByUsername = teacherMapper.selectByPrimaryKey(username);
        Teacher teacherByTel = teacherMapper.selectByTel(username);

        // 合并结果，优先使用查找到的教师信息
        Teacher teacher = teacherByUsername != null ? teacherByUsername : teacherByTel;

        // 如果未找到用户或者账号状态为禁用
        if (teacher == null || teacher.getAccountStatus() == 0) {
            return null;
        }

        // 验证密码
        if (teacher.getTpwd().equals(Md5Util.encrypt(password))) {
            // 构建JWT的Claims
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", teacher.getTno());
            claims.put("name", teacher.getTname());
            claims.put("role", "teacher");

            // 生成Token
            String token = JwtUtil.generateToken(claims);

            // 返回登录信息
            return new LoginInfo(teacher.getTno(), teacher.getTname(), token);
        }

        // 密码验证失败
        return null;
    }
}
